<%@page import="entities.Application"%>
<%@page import="entities.User"%>
<%@page import="entities.Category"%>
<%@page import="java.util.Map"%>
<%@page import="helpers.DateHelper"%>
<%@page import="helpers.StringHelper"%>
<%@page import="entities.Article"%>
<%@page import="java.util.List"%>
<%@page import="helpers.UrlHelper"%>
<%
String id = (String) request.getAttribute("id");
String title = (String) request.getAttribute("title");
Application app = (Application) request.getAttribute("app");

List<Article> articles = (List<Article>) request.getAttribute("articles");
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories");
Map<String, User> authors = (Map<String, User>) request.getAttribute("authors");
%>

<jsp:include page="/admin/layouts/header.jsp" />
   
<script type="text/javascript" src="/admin/js/article_list.js"></script>
<script type="text/javascript" src="/admin/js/list_form.js"></script>
<script type="text/javascript">
<!--
var server = "<%=UrlHelper.getServerUrl(request)%>";
//-->
</script>
        <div class="maincontent noright">
        	<div class="maincontentinner">
            	
                <ul class="maintabmenu">
                	<li><a href="/admin/article/list/<%=app.getId()%>">Article Manager</a></li>
                	<li class="current"><a>Search Article</a></li>
                	<li><a href="/admin/article/add/<%=app.getId()%>">Add New Article</a></li>
                </ul><!--maintabmenu-->
                
                <div class="content">
                	<jsp:include page="/admin/elements/message_box.jsp" />
                	
                	<form method="post" action="" class="stdform">
	                    <div class="one_half">
	                    	<p>
	                    		<label>Article Id : </label>
	                            <span class="field"><input type="text" value="<%=id %>" class="longinput" name="id"></span>                
	                    	</p>
	                    	<p>
	                    		<label>Article Title : </label>
	                    		<span class="field"><input type="text" value="<%=title %>" class="longinput" name="title"></span>                  
	                    	</p>                               	
	                    	<p class="stdformbutton">
		                     	<input type="submit" value="Search" class="reset radius2">
		                        <input type="reset" value="Reset" class="reset radius2">
		                     </p>                  	     	                    	        	                                        
	                    </div><!--one_half-->     
	                    <div class="one_half last">
	                    	     
	                    </div>               
                 		
                 		<br clear="all">
                 		
                 		<div class="contenttitle radiusbottom0">
		                	<h2 class="table"><span>List Article</span></h2>
		                </div>
                 		<table cellspacing="0" cellpadding="0" border="0" class="stdtable stdtablecb" id="list-table">
		                    <colgroup>
		                        <col class="con0">
		                        <col class="con1">
		                        <col class="con0">
		                        <col class="con1">
		                        <col class="con0">
		                        <col class="con1">
		                        <col class="con0">
		                    </colgroup>
		                    <thead>
		                        <tr>
		                        	<th class="head0"><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
		                            <th class="head1">Title</th>
		                            <th class="head0">Author</th>
	                                <th class="head1">Category</th>
		                            <th class="head0">Publish</th>
		                            <th class="head1">Created</th>
		                            <th class="head0">Action</th>
		                        </tr>
		                    </thead>
		                    <tfoot>
		                        <tr>
		                        	<th class="head0"><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
		                            <th class="head1">Title</th>
		                            <th class="head0">Author</th>
	                                <th class="head1">Category</th>
		                            <th class="head0">Publish</th>
		                            <th class="head1">Created</th>
		                            <th class="head0">Action</th>
		                        </tr>
		                    </tfoot>
		                    <tbody>
	                    	<%
	                    	if (articles != null) {
	                    		for (int i = 0; i < articles.size(); i++) { 
	                    	%>
	                        <tr>
	                        	<td class="center"><span class="checkbox"><input type="checkbox" value="<%=articles.get(i).getId() %>"></span></td>
	                            <td><%=StringHelper.cutString(articles.get(i).getTitle(), 15) %></td>
	                            <td class="center">
	                            	<% if (!articles.get(i).getAuthorName().isEmpty()) {%>
	                            	<%=articles.get(i).getAuthorName() %>
	                            	<% } else if (authors.get(articles.get(i).getCreator()) != null) { %>
	                            		<%=authors.get(articles.get(i).getCreator()).getName() %>
	                            	<% } %>
	                            </td>
	                            <td class="center">
	                            	<% if (categories.get(articles.get(i).getCategory()) != null) { %>
	                            	<%=categories.get(articles.get(i).getCategory()).getName() %>
	                            	<% } %>
	                            </td>
	                            <td class="center">
	                            	<%
	                            	switch (articles.get(i).getStatus()) {
	                            		case 0: 
	                            			%>
	                            			Unpublish
	                            			<%
	                            			break;
	                            		case 1:
	                            			%>
	                            			Publish
	                            			<%
	                            			break;
	                            		case 2:
	                            			%>
	                            			UnReview
	                            			<%
	                            			break;
	                            	} 
	                            	%>
	                            </td>
	                            <td class="center"><%=DateHelper.getStringFromDate(articles.get(i).getCreated(), "dd-MM-yyyy")  %></td>
	                            <td class="center">
									<a title="Edit Article" href="/admin/article/edit/<%=articles.get(i).getId() %>" class="edit">
										Edit
									</a>&nbsp;
									<a title="Remove Article" href="/admin/article/delete/<%=articles.get(i).getId() %>" id="action_del" class="delete">
										Delete
									</a>
								</td>
	                        </tr>
	                        <%   } 
	                        } %>
	                    </tbody>
		                </table>
                   </form>
	                
	                
                </div><!--content-->
                
            </div><!--maincontentinner-->
            
            <jsp:include page="/admin/elements/copyright.jsp" />
            
        </div><!--maincontent-->
 
 <jsp:include page="/admin/layouts/footer.jsp" />